﻿namespace JxcLite.Extensions;

static class MaxNoExtension
{
    internal static async Task<string> GetMaxBillNoAsync(this Database db, string type)
    {
        var prefix = "T";
        if (type == BillType.Import) prefix = "IM";
        else if (type == BillType.ImportReturn) prefix = "IR";
        else if (type == BillType.Export) prefix = "EX";
        else if (type == BillType.ExportReturn) prefix = "ER";
        prefix += $"{DateTime.Now:yyyy}";
        var maxNo = await BillRepository.GetMaxBillNoAsync(db, prefix);
        if (string.IsNullOrWhiteSpace(maxNo))
            maxNo = $"{prefix}0000";
        return Utils.GetMaxFormNo(prefix, maxNo);
    }

    internal static async Task<string> GetMaxAccountNoAsync(this Database db, string type)
    {
        var prefix = type == PartnerType.Supplier ? "SA" : "CA";
        prefix += $"{DateTime.Now:yyyy}";
        var maxNo = await FinanceRepository.GetMaxAccountNoAsync(db, prefix);
        if (string.IsNullOrWhiteSpace(maxNo))
            maxNo = $"{prefix}0000";
        return Utils.GetMaxFormNo(prefix, maxNo);
    }
}